/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * Project Name: lmrp-android framework
 * Create Time: 16-2-16 下午6:50
 */

package com.lenovo.framework.rest.client;

import com.lenovo.framework.FApplication;
import com.lenovo.framework.biz.ContextManager;
import com.lenovo.framework.biz.IAccountManager;
import com.lenovo.framework.conf.FConstants;
import com.lenovo.framework.rest.CountingRequestBody;
import com.lenovo.framework.rest.DownloadProgressInterceptor;
import com.lenovo.framework.rest.RefreshableAuthInterceptor;
import com.lenovo.framework.rest.TimberLoggingInterceptor;
import com.lenovo.framework.rest.UpLoadProgressInterceptor;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * httpclient suitable for https&http connection but client is not certificated
 * @author hejie5
 * @version 1.0
 * @updated 16-2月-2016 18:02:54
 */
public class OneWayAuthHttpClient {
    private static OkHttpClient okHttpClient;
    private static OkHttpClient glideClient;

    private static Interceptor headerInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request.Builder builder = chain.request().newBuilder();
            return chain.proceed(builder.build());
        }
    };

    public static synchronized OkHttpClient getGlideClient() {
        if(glideClient == null) {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(FConstants.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
                    .readTimeout(FConstants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                    .retryOnConnectionFailure(false)
                    .addInterceptor(headerInterceptor);
            if(FApplication.DEBUG) {
                builder.addInterceptor(new TimberLoggingInterceptor());
            }
            glideClient = builder.build();
        }
        return glideClient;
    }

    public static synchronized OkHttpClient getClient(IAccountManager accountService, CountingRequestBody.Listener uploadListener, DownloadProgressInterceptor.DownloadProgressListener downloadProgressListener) {
        if(accountService != null && okHttpClient != null && uploadListener == null && downloadProgressListener == null)
            return okHttpClient;
        Cache cache = new Cache(new File(ContextManager.context().getCacheDir(), FConstants.HTTP_CACHE_DIR), 1024 * 1024 * 50);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(FConstants.CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(FConstants.HTTP_READ_TIMEOUT, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(false)
                .cache(cache)
                .addInterceptor(new RefreshableAuthInterceptor(accountService));
        if(uploadListener != null)
            builder.addNetworkInterceptor(new UpLoadProgressInterceptor(uploadListener));
        if(downloadProgressListener != null)
            builder.addNetworkInterceptor(new DownloadProgressInterceptor(downloadProgressListener));
        if(FApplication.DEBUG) {
            builder.addInterceptor(new TimberLoggingInterceptor());
        }
        //common okhttpclient
        if(accountService != null && uploadListener == null && downloadProgressListener == null)
            return okHttpClient = builder.build();
            //return new okhttpclient
        else
            return  builder.build();
    }

    public static synchronized OkHttpClient getClient(IAccountManager accountService) {
        return getClient(accountService, null, null);
    }
}
